const BehaviorAll = require("../../../behaviors/behavior_all.js");
const ServiceProduct = require("../../../service/service_product.js");
const ServiceScore = require("../../../service/service_score.js");
const ServiceUser = require("../../../service/service_user.js");

Component({
  /**
   * behaviors
   */
  behaviors: [BehaviorAll],
  /**
   * 页面的初始数据
   */
  data: {
    allScore:0,
    /**
     * 产品
     * 数据格式：
        [
          { id: 1, name: "法国进口红酒拉斐天使庄园干红葡萄酒红酒整箱礼盒装750ml*6", price: 100,
            imgUrl: 'xxx.jpg',
            properties: [
              { id: 1, name: "葡萄酒" },
              { id: 2, name: "干红" },
              { id: 3, name: "750ml" },
              { id: 4, name: "法国" },
              { id: 5, name: "6只装" }
            ]
          }
        ]
    */
    products: [],
    categories:[],
    selectedCategoryId: null,
    /**
     * 开始页
     */
    startPage: 0,
    types: ['易物空间'],
    /**
     * 购物车相关
     */
    triggerAddData: null,
    triggerRemoveData: null,
    triggerRetrieveData: null,
    car_amount: 0,
    remainTotalScore: 0,  // 剩余总积分
    allDataMap: {}
  },

  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const _this = this;
      // 加载数据
      ServiceScore.find(function (data) {
        _this.setData({
          allScore: data
        })
      })
      ServiceUser.getInfo(function (data) {
        _this.setData({
          userInfo: data
        })
      });
      ServiceProduct.categoryList('易物空间', function(data){
        _this.setData({
          categories: data.records
        });
      });

      ServiceProduct.page(0, _this.data.types, [], {}, function (data) {
        _this.setData({
          products: data.records
        });
      });
      // 处理剩余总积分
      const products = _this.data.products;
      if (products.length>0){
        _this.setData({
          remainTotalScore: products[0].totalScore
        });
      }
    },
    /**
     * onShow
     */
    onShow: function(e){
      const _this = this;
      _this.setData({ triggerRetrieveData: true });
    },
    /**
     * 添加到购物车
     */
    onAdd: function(e){
      const _this = this;
      const productId = e.currentTarget.dataset.id;
      ServiceProduct.findById(productId, function(data){
        _this.setData({
          triggerAddData: { 
              id: data.id, 
              name: data.name,
              value: data.score, 
              amount: data.amount, 
              imgUrl: data.imgUrl,
              amount: 1,
              selected: true,
              describe: '每件需' + data.score + '易物值'
            }
        })
      });
    },
    /**
     * 从购物车移除
     */
    onRemove: function (e) {
      const _this = this;
      const productId = e.currentTarget.dataset.id;
      _this.setData({
        triggerRemoveData: { id: productId}
      })
    },
    /**
     * 数据改变
     */
    car_onChange: function (e) {
      const _this = this;
      _this.setData({
        car_amount: e.detail.amount,
        allDataMap: e.detail.allDataMap
      })
    },
    /**
     * 获取数据
     */
    car_onRetrieve: function (e) {
      const _this = this;
      _this.setData({
        car_amount: e.detail.amount,
        allDataMap: e.detail.allDataMap
      })
    },
    /**
     * onSelectCategory
     */
    onSelectCategory: function(e){
      const _this = this;
      const categoryId = e.currentTarget.dataset.id;
      wx.pageScrollTo({ scrollTop: 0 })
      ServiceProduct.page(0, _this.data.types, [], { categoryId }, function (data) {
        _this.setData({
          products: data.records,
          selectedCategoryId: categoryId,
          startPage: 0
        });
      });
    },
    /**
     * 触底事件
     */
    onReachBottom: function () {
      const _this = this;
      let startPage = _this.data.startPage + 1;
      const categoryId = _this.data.selectedCategoryId;
      ServiceProduct.page(startPage, _this.data.types, [], { categoryId }, function (data) {
        _this.data.products.push(...data.records);
        _this.setData({
          products: _this.data.products,
          startPage: startPage
        });
      });
    }
  }

})